package middleware

import (
	"github.com/gin-gonic/gin"
	"github.com/vueadmin/utils/response"
	"go.uber.org/zap"
	"net/http"
)

func RecoverMiddleware(log *zap.SugaredLogger) gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			if err := recover(); err != nil {
				log.Error("[Recovery from panic]",
					zap.Any("error", err),
					zap.String("method", c.Request.Method),
					zap.String("request", c.Request.RequestURI),
				)
				response.FailWithCode(c, http.StatusInternalServerError, "系统错误!")
				c.Abort()
				return
			}
		}()
		c.Next()
	}
}
